<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item label="姓名" size="small">
            <el-input v-model="searchForm.insuredName"></el-input>
          </el-form-item>
        </el-col>
        <el-col v-if="roleType===0" class="other">
          <el-form-item label="团队/部门" size="small">
            <el-autocomplete
              v-model="searchForm.tertiarySector"
              :fetch-suggestions="querySearchTeam"
              @select="handleSelectTeam"
              placeholder="请输入内容"
            >
            </el-autocomplete>
          </el-form-item>
        </el-col>
        <!--<el-col class="other">-->
        <!--<el-form-item label="参保单位" size="small">-->
        <!--<el-input v-model="searchForm.insuredUnit"></el-input>-->
        <!--</el-form-item>-->
        <!--</el-col>-->
        <el-col class="singleDatePicker">
          <el-form-item label="业务时间" size="small">
            <el-date-picker
              v-model="searchForm.serviceTime"
              type="month"
              value-format="timestamp"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!--<div class="table-btn">-->
      <!--<el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()">添加</el-button>-->
      <!--<el-date-picker-->
        <!--v-model="finalMonth"-->
        <!--type="month"-->
        <!--value-format="timestamp"-->
        <!--placeholder="选择日期"-->
        <!--size="small"-->
      <!--&gt;-->
      <!--</el-date-picker>-->
      <!--<el-button type="primary" size="small" @click="addSocialSecurityFee()" :disabled="!finalMonth">生成缴费名单</el-button>-->
      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    <!--</div>-->
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
    >
      <el-table-column
        slot="opt"
        fixed="right"
        label="操作"
        width="120">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.socialId)"
            type="text"
            size="small">
            修改
          </el-button>
          <!--<el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>-->
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="1000px">
      <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="110px">
        <el-row>
          <el-col class="singleDatePicker">
            <!--<el-form-item label="员工编号" size="small">-->
            <!--{{teamRow.employeeCode}}-->
            <!--</el-form-item>-->
            <el-form-item label="姓名" size="small">
              {{teamRow.insuredName}}
            </el-form-item>
            <!--<el-form-item label="职级" size="small">-->
              <!--{{teamRow.rank | filterRank}}-->
            <!--</el-form-item>-->
          </el-col>
          <el-col class="singleDatePicker">
            <el-form-item label="团队/部门" size="small">
              {{teamRow.teamName}}
            </el-form-item>
          </el-col>
          <el-col class="singleDatePicker">
            <el-form-item label="职务" size="small">
              {{teamRow.position}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="社保" size="small">
        </el-form-item>
        <el-row>
          <el-col class="singleDatePicker">
            <el-form-item prop="insuredStatus" label="参保状态" size="small">
              <el-select v-model="teamRow.insuredStatus" placeholder="请选择">
                <el-option
                  v-for="item in global.insuredStatusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="insuredBase" v-if="teamRow.insuredStatus===1" label="缴费基数" size="small"
                          class="item-inline">
              <el-input type="number" v-model.number="teamRow.insuredBase">
                <span slot="suffix">元</span>
              </el-input>
            </el-form-item>
            <el-form-item v-if="teamRow.insuredStatus===1" label="业务时间" size="small">
              <el-date-picker
                v-model="teamRow.serviceTime"
                type="month"
                value-format="timestamp"
                placeholder="选择日期"
                :disabled="isServiceTime"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="singleDatePicker">
            <el-form-item prop="insuredUnit" v-if="teamRow.insuredStatus===1" label="参保单位" size="small"
                          class="item-inline">
              <el-select v-model="teamRow.insuredUnit" placeholder="请选择">
                <el-option
                  v-for="item in global.insuredUnitList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="finishTime" v-if="teamRow.insuredStatus===1" label="办结时间" size="small">
              <el-date-picker
                v-model="teamRow.finishTime"
                type="date"
                value-format="timestamp"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="singleDatePicker">
            <el-form-item prop="startTime" v-if="teamRow.insuredStatus===1" label="参保时间" size="small">
              <el-date-picker
                v-model="teamRow.startTime"
                type="month"
                value-format="timestamp"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item prop="finishBy" v-if="teamRow.insuredStatus===1" label="办结人" size="small" class="item-inline">
              <el-input type="text" v-model="teamRow.finishBy"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="公积金" size="small">
        </el-form-item>
        <el-row>
          <el-col class="singleDatePicker">
            <el-form-item prop="insuredStatus1" label="参缴状态" size="small">
              <el-select v-model="teamRow.insuredStatus1" placeholder="请选择">
                <el-option
                  v-for="item in global.insuredStatusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="insuredBase1" v-if="teamRow.insuredStatus1===1" label="缴费基数" size="small"
                          class="item-inline">
              <el-input type="number" v-model.number="teamRow.insuredBase1">
                <span slot="suffix">元</span>
              </el-input>
            </el-form-item>
            <el-form-item v-if="teamRow.insuredStatus1===1" label="业务时间" size="small">
              <el-date-picker
                v-model="teamRow.serviceTime1"
                type="month"
                value-format="timestamp"
                placeholder="选择日期"
                :disabled="isServiceTime1"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="singleDatePicker">
            <el-form-item prop="insuredUnit1" v-if="teamRow.insuredStatus1===1" label="参缴单位" size="small"
                          class="item-inline">
              <el-select v-model="teamRow.insuredUnit1" placeholder="请选择">
                <el-option
                  v-for="item in global.insuredUnitList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="finishTime1" v-if="teamRow.insuredStatus1===1" label="办结时间" size="small">
              <el-date-picker
                v-model="teamRow.finishTime1"
                type="date"
                value-format="timestamp"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="singleDatePicker">
            <el-form-item prop="startTime1" v-if="teamRow.insuredStatus1===1" label="参缴时间" size="small">
              <el-date-picker
                v-model="teamRow.startTime1"
                type="month"
                value-format="timestamp"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item prop="finishBy1" v-if="teamRow.insuredStatus1===1" label="办结人" size="small"
                          class="item-inline">
              <el-input type="text" v-model="teamRow.finishBy1"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
        <el-button size="small" @click="outerVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    addMoney,
    deleteMoney,
    getList,
    selectMoney,
    selectTeam,
    updateMoney,
    updateSocialSecurityFee,
    addSocialSecurityFee
  } from '@/api/socialsecurity'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const configs = {
    props: ['colConfig'],
    filters: {
      formatMonth(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyyMM')
      },
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyyMMdd')
      },
      formatTime(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      },
      filterInsuredStatus(type) {
        if (type) {
          let obj = {}
          obj = global.insuredStatusList.find((item) => {
            return item.value === type // 筛选出匹配数据
          })
          return obj.label
        }
      },
      filterInsuredUnit(type) {
        if (type) {
          let obj = {}
          obj = global.insuredUnitList.find((item) => {
            return item.value === type // 筛选出匹配数据
          })
          return obj.label
        }
      }
    },
    template: `
  <el-table-column :label="colConfig.label">
            <el-table-column  v-for="(item, index) in colConfig.children"  :label="item.label" :width="item.width">
                <span slot-scope="scope" >
                    <span v-if="item.component==='month'"> {{ scope.row[item.prop] | formatMonth }}</span>
                    <span v-else-if="item.component==='time'"> {{ scope.row[item.prop] | formatDate }}</span>
                    <span v-else-if="item.component==='insuredStatus'"> {{ scope.row[item.prop] | filterInsuredStatus }}</span>
                    <span v-else-if="item.component==='insuredUnit'"> {{ scope.row[item.prop] | filterInsuredUnit }}</span>
                    <span v-else >{{scope.row[item.prop]}}</span>
                </span>
            </el-table-column>
  </el-table-column>
  `
  }

  const formatter = function(row, column) {
    if (row[column.property]) {
      let obj = {}
      obj = global[`${column.property}List`].find((item) => {
        return item.value === row[column.property] // 筛选出匹配数据
      })
      return obj.label
    }
  }

  const tertiarySector = function(row, column) {
    if (row.tertiarySector) {
      return row.tertiarySector
    } else {
      if (row[column.property]) {
        let obj = {}
        obj = global[`${column.property}List`].find((item) => {
          return item.value === row[column.property] // 筛选出匹配数据
        })
        return obj.label
      }
    }
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd')
      },
      filterRank(rank) {
        if (rank) {
          let obj = {}
          obj = global.rankList.find((item) => {
            return item.value === rank // 筛选出匹配数据
          })
          return obj.label
        }
      }
    },
    data() {
      return {
        colConfigs: [
          // { prop: 'employeeCode', label: '员工编号', width: '150', fixed: true },
          { prop: 'secondarySector', label: '部门', width: '100', fixed: true, formatter: tertiarySector },
          { prop: 'insuredName', label: '姓名', width: '150', fixed: true },
          { prop: 'position', label: '职务', width: '150' },
          // { prop: 'rank', label: '职级', width: '150', formatter: formatter },
          {
            label: '社保', width: '900', children: [
              { prop: 'insuredStatus', label: '参保状态', width: '150', component: 'insuredStatus' },
              { prop: 'insuredUnit', label: '参保单位', width: '150', component: 'insuredUnit' },
              { prop: 'startTime', label: '参保时间', width: '150', component: 'month' },
              { prop: 'insuredBase', label: '缴费基数', width: '150' },
              { prop: 'serviceTime', label: '业务时间', width: '150', component: 'month' },
              { prop: 'finishTime', label: '办结时间', width: '150', component: 'time' },
              { prop: 'finishBy', label: '办结人', width: '150' }
            ], component: configs
          },
          {
            label: '公积金', width: '900', children: [
              { prop: 'insuredStatus1', label: '参保状态', width: '150', component: 'insuredStatus' },
              { prop: 'insuredUnit1', label: '参保单位', width: '150', component: 'insuredUnit' },
              { prop: 'startTime1', label: '参保时间', width: '150', component: 'month' },
              { prop: 'insuredBase1', label: '缴费基数', width: '150' },
              { prop: 'serviceTime1', label: '业务时间', width: '150', component: 'month' },
              { prop: 'finishTime1', label: '办结时间', width: '150', component: 'time' },
              { prop: 'finishBy1', label: '办结人', width: '150' }
            ], component: configs
          }
        ],
        searchForm: {},
        searchFormList: {},
        tenderNames: [],
        infoTitles: [],
        listLoading: true,
        dialogTitle: '新增社保公积金',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        innerVisible: false,
        pca: pca,
        rules: {
          insuredStatus: [
            { required: true, message: '请选择社保参保状态', trigger: 'change' }
          ],
          insuredUnit: [
            { required: true, message: '请选择社保参保单位', trigger: 'change' }
          ],
          startTime: [
            { required: true, message: '请输入社保参保时间', trigger: 'change' }
          ],
          insuredBase: [
            { required: true, message: '请选择社保缴费基数', trigger: 'change' }
          ],
          finishTime: [
            { required: true, message: '请选择办结时间', trigger: 'change' }
          ],
          finishBy: [
            { required: true, message: '请输入办结人', trigger: 'change' }
          ],
          serviceTime: [
            { required: true, message: '请选择业务时间', trigger: 'change' }
          ],
          insuredStatus1: [
            { required: true, message: '请选择公积金参保状态', trigger: 'change' }
          ],
          insuredUnit1: [
            { required: true, message: '请选择公积金参保单位', trigger: 'change' }
          ],
          startTime1: [
            { required: true, message: '请输入公积金参保时间', trigger: 'change' }
          ],
          insuredBase1: [
            { required: true, message: '请选择公积金缴费基数', trigger: 'change' }
          ],
          finishTime1: [
            { required: true, message: '请选择办结时间', trigger: 'change' }
          ],
          finishBy1: [
            { required: true, message: '请输入办结人', trigger: 'change' }
          ],
          serviceTime1: [
            { required: true, message: '请选择业务时间', trigger: 'change' }
          ]
        },
        visible: false,
        isServiceTime: true,
        isServiceTime1: true,
        finalMonth: ''
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      global() {
        return global
      },
      teamId() {
        return this.$store.state.user.teamId
      },
      roleType() {
        return this.$store.state.user.roleType
      },
      teamIds() {
        return this.$store.state.user.teamIds
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.searchFormList.roleType = this.roleType
        this.searchFormList.teamId = this.teamId
        this.searchFormList.teamIds = this.teamIds
        this.searchFormList.sociaType = 1
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchForm.departmentName = this.searchForm.secondarySector ? this.searchForm.secondarySector : this.searchForm.tertiarySector
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        delete this.searchFormList.secondarySector
        delete this.searchFormList.tertiarySector
        this.fetchData()
      },
      addSocialSecurityFee() {
        const params = {
          finalMonth: this.finalMonth
        }
        addSocialSecurityFee(params).then(response => {
        })
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '新增社保公积金'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.teamRow.isTender = 1
        this.dialogType = 1
      },
      editTeam(socialId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改社保公积金'
        this.dialogType = 0
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        const params = {
          socialId: socialId
        }
        selectMoney(params).then(response => {
          const data = response.responseBody
          this.teamRow = Object.assign({}, this.teamRow, data)
          this.isServiceTime = !!this.teamRow.serviceTime
          this.isServiceTime1 = !!this.teamRow.serviceTime1
        })
      },
      submitForm(formName) {
        const params = {
          sociaType: 1,
          employeeId: this.teamRow.employeeId,
          insuredStatus: this.teamRow.insuredStatus,
          insuredStatus1: this.teamRow.insuredStatus1
        }
        if (this.teamRow.insuredStatus === 1) {
          params.insuredBase = this.teamRow.insuredBase
          params.insuredUnit = this.teamRow.insuredUnit
          params.finishTime = this.teamRow.finishTime
          params.startTime = this.teamRow.startTime
          params.finishBy = this.teamRow.finishBy
          params.serviceTime = this.teamRow.serviceTime
        }
        if (this.teamRow.insuredStatus1 === 1) {
          params.insuredBase1 = this.teamRow.insuredBase1
          params.insuredUnit1 = this.teamRow.insuredUnit1
          params.finishTime1 = this.teamRow.finishTime1
          params.startTime1 = this.teamRow.startTime1
          params.finishBy1 = this.teamRow.finishBy1
          params.serviceTime1 = this.teamRow.serviceTime1
        }
        if (!this.dialogType) {
          params.socialId = this.teamRow.socialId
          params.socialId1 = this.teamRow.socialId1
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              addMoney(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updateMoney(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          socialId: scope.row.socialId,
          isDel: 1
        }
        deleteMoney(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      querySearchTeam(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        delete this.searchForm.secondarySector
        callback(this.global.secondarySectorListSearch)
      },
      handleSelectTeam(item) {
        this.$set(this.searchForm, 'secondarySector', item.label)
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
